#!/usr/bin/python

from windows import AddServerWindow, EditServerWindow

import pygtk
pygtk.require('2.0')
import gtk
import ConfigParser
import subprocess
import shlex
import appindicator

from keyring import Keyring
from serverdb import ServerDB

class PyRdpTray:
    def __init__(self):
        self.config = ConfigParser.ConfigParser()
        self.config.read('etc/pyrdp.conf')
        self.name = "pyrdp"
        self.protocol = "rdp"
        self.server = "pyrdp"
        
        # Load Server database
        self.serverdb = ServerDB()
        self.serverdb.read_file(self.config.get("pyrdp", "serverdb"))
        
        # Load Gnome Keyring via API
        self.keyring = Keyring(self.name, self.protocol)
        if self.config.get('pyrdp', 'desktop') == "unity":
        
            self.statusIcon = appindicator.Indicator ("pyrdp", "gnome-ccwindowmanager", appindicator.CATEGORY_APPLICATION_STATUS)
            self.statusIcon.set_status (appindicator.STATUS_ACTIVE)
            self.statusIcon.set_attention_icon ("indicator-messages-new")
        else:
            self.statusIcon = gtk.StatusIcon()
            self.statusIcon.set_from_stock(gtk.STOCK_NETWORK)
            self.statusIcon.set_visible(True)
            self.statusIcon.set_tooltip("pyrdp")
		
        self.menu = gtk.Menu()
        for server in self.serverdb.get_servers():
            item = gtk.ImageMenuItem(server.common_name)
            item.set_name(server.hostname)
            img = gtk.Image()
            img.set_from_file('img/rdp.png')
            item.set_image(img)

            item.connect('activate', self.run_cmd, self.statusIcon)
            item.show()
            self.menu.append(item)
        
        self.menuItem = gtk.SeparatorMenuItem()
        self.menu.append(self.menuItem)
        
        umenu = gtk.Menu()
        usermenu = gtk.MenuItem("Users")
        usermenu.show()
        usermenu.set_submenu(umenu) 
        
        for user in self.keyring.get_credentials_list(self.server):
            item = gtk.ImageMenuItem(user.attributes["user"])
            item.set_name(str(user.item_id))
            item.connect('activate', self.run_cmd, self.statusIcon)
            item.show()
            umenu.append(item)    
        self.menu.append(usermenu)    
        
        self.menuItem = gtk.ImageMenuItem("Add Server")
        self.menuItem.set_name("addserver")
        self.menuItem.connect('activate', self.add_server_form, self.statusIcon)
        self.menuItem.show()
        self.menu.append(self.menuItem)

        self.menuItem = gtk.ImageMenuItem("Edit Servers")
        self.menuItem.set_name("editserver")
        self.menuItem.connect('activate', self.edit_server_form, self.statusIcon)
        self.menuItem.show()
        self.menu.append(self.menuItem)
        
        self.menuItem = gtk.ImageMenuItem(gtk.STOCK_QUIT)
        self.menuItem.connect('activate', self.quit_pyrdp, self.statusIcon)
        self.menuItem.show()
        self.menu.append(self.menuItem)
        if self.config.get('pyrdp', 'desktop') == "unity":
		    self.statusIcon.set_menu(self.menu)
        else:
            self.statusIcon.connect('popup-menu', self.popup_menu_cb, self.menu)
            self.statusIcon.set_visible(1)
             
        gtk.main()

    def add_server_form(self, widget, test):
        AddServerWindow(self.serverdb, self.keyring)
 
    def edit_server_form(self, widget, test):
        EditServerWindow(self.serverdb, self.keyring)
        
    def run_cmd(self, widget, test):
        server = self.serverdb.lookup(widget.name)
        if server != None:
            cred = self.keyring.get_credentials(self.server, server.get_default_user())
            if cred != None:
                cmd = self.config.get("rdesktop", "cmd") + " -g " + self.config.get("rdesktop", "resolution") + " -" + self.config.get("rdesktop", "version") + " -u " + cred.attributes["user"] + " -p " + cred.secret + " " + server.get_hostname()
                subprocess.Popen(shlex.split(cmd))
            else:
                print "User: " + server.get_default_user() + " not found in gnomekeyring database."
        else:
            print "Server: " + widget.name + " not found in server database."
                
    def quit_pyrdp(self, widget, data = None):
        gtk.main_quit()

    def popup_menu_cb(self, widget, button, time, data = None):
        if button == 3:
            if data:
                data.show_all()
                data.popup(None, None, gtk.status_icon_position_menu,
                       3, time, self.statusIcon)
           
if __name__ == "__main__":
    pyrdptray = PyRdpTray()
